
// Copyright (C) 2012 Luca Piccioni
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//  
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Xml;

namespace Derm.Xml.Collada
{
	/// <summary>
	/// The library_nodes element declares a module of node elements.
	/// </summary>
	public class ColladaLibraryNodes : XmlSerializableExt
	{
		#region Library Node Information

		/// <summary>
		/// Nodes library identifier.
		/// </summary>
		public string Id;

		/// <summary>
		/// Nodes library name.
		/// </summary>
		public string Name;

		/// <summary>
		/// Nodes library asset.
		/// </summary>
		public ColladaAsset Asset;

		#endregion

		#region XmlSerializableExt Overrides

		/// <summary>
		/// Read the attribute of the current XML serializable.
		/// </summary>
		/// <param name="xmlReader">
		/// A <see cref="XmlReader"/> used for reading XML element attributes.
		/// </param>
		/// <remarks>
		/// <para>
		/// This method is called only in the case there are effectively attributes on XML element. Don't expect that this
		/// method will be called even when the attributes count is zero. This is very important, since the design doesn't
		/// allow object initialization using this method.
		/// </para>
		/// </remarks>
		public override void ReadAttributes(XmlReader xmlReader)
		{
			if (xmlReader == null)
				throw new ArgumentNullException("xmlReader");

			string attribute;

			if ((attribute = xmlReader[ColladaAttributeId]) != null)
				Id = attribute;
			if ((attribute = xmlReader[ColladaAttributeName]) != null)
				Name = attribute;
		}

		/// <summary>
		/// Read an element of the current XML serializable.
		/// </summary>
		/// <param name="elementName">
		/// A <see cref="System.String"/> that specify the element name.
		/// </param>
		/// <param name="xmlReader">
		/// A <see cref="XmlReader"/> used for reading child element. It is positioned at the start element.
		/// </param>
		/// <remarks>
		/// <para>
		/// This method is responsible to read the start element of the nested element.
		/// </para>
		/// <para>
		/// This method is resposible to read all children elements till the corresponding end element, without reading it. In the
		/// case this condition is not satisfied, the XML parsing will be aborted.
		/// </para>
		/// </remarks>
		public override void ReadElement(string elementName, XmlReader xmlReader)
		{
			if (elementName == null)
				throw new ArgumentNullException("xmlReader");
			if (xmlReader == null)
				throw new ArgumentNullException("xmlReader");

			if (elementName == ColladaElementAsset) {
				Asset = new ColladaAsset();
				Asset.ReadXml(xmlReader);
			} else if (elementName == ColladaElementNode) {
				XmlHelper.SkipXmlElement(xmlReader);
			} else if (elementName == ColladaElementExtra) {
				XmlHelper.SkipXmlElement(xmlReader);
			} else
				XmlHelper.SkipXmlElement(xmlReader);
		}

		/// <summary>
		/// Collada 'library_images' attribute name (1.4).
		/// </summary>
		private static readonly string ColladaAttributeId = "id";

		/// <summary>
		/// Collada 'library_images' attribute name (1.4).
		/// </summary>
		private static readonly string ColladaAttributeName = "name";

		/// <summary>
		/// Collada 'library_images' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementAsset = "asset";

		/// <summary>
		/// Collada 'library_images' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementNode = "node";

		/// <summary>
		/// Collada 'library_images' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementExtra = "extra";

		#endregion
	}
}
